package com.ksd.modules.special.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.enums.DeletedEnums;
import com.ksd.common.enums.StatusEnums;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.special.entity.AdminSpecial;
import com.ksd.modules.special.mapper.AdminSpecialMapper;
import com.ksd.modules.special.po.AdminSpecialSavePO;
import com.ksd.modules.special.po.AdminSpecialSearchPO;
import com.ksd.modules.special.service.IAdminSpecialService;
import com.ksd.modules.special.vo.AdminSpecialVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdminSpecialServiceImpl extends BaseServiceImpl<AdminSpecialMapper, AdminSpecial> implements IAdminSpecialService {


    /**
     * 查询列表
     * @param adminSpecialSearchPO
     * @return
     */
    public Page<AdminSpecialVo> findAdminSpecials(AdminSpecialSearchPO adminSpecialSearchPO){
        Page<AdminSpecial> page = new Page<>(adminSpecialSearchPO.getPageNo(),adminSpecialSearchPO.getPageSize());
        LambdaQueryWrapper<AdminSpecial> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(AdminSpecial.class,c -> !c.getColumn().equalsIgnoreCase("content"));
        lambdaQueryWrapper.eq(AdminSpecial::getIsDeleted, DeletedEnums.UN_DELETED.getCode());
        lambdaQueryWrapper.eq(AdminSpecial::getSystemId,SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialSearchPO.getStatus()), AdminSpecial::getStatus,adminSpecialSearchPO.getStatus());
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialSearchPO.getSpecialtype()), AdminSpecial::getSpecialtype,adminSpecialSearchPO.getSpecialtype());
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialSearchPO.getNewFlag()), AdminSpecial::getNewFlag,adminSpecialSearchPO.getNewFlag());
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialSearchPO.getHotFlag()), AdminSpecial::getHotFlag,adminSpecialSearchPO.getHotFlag());
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialSearchPO.getPushFlag()), AdminSpecial::getPushFlag,adminSpecialSearchPO.getPushFlag());
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialSearchPO.getCategoryPid()), AdminSpecial::getCategoryPid,adminSpecialSearchPO.getCategoryPid());
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialSearchPO.getCategoryId()), AdminSpecial::getCategoryId,adminSpecialSearchPO.getCategoryId());
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialSearchPO.getCommentFlag()), AdminSpecial::getCommentFlag,adminSpecialSearchPO.getCommentFlag());
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialSearchPO.getMode()), AdminSpecial::getMode,adminSpecialSearchPO.getMode());
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialSearchPO.getVipFlag()), AdminSpecial::getVipFlag,adminSpecialSearchPO.getVipFlag());
        // 模糊匹配
        if(StringUtils.isNotEmpty(adminSpecialSearchPO.getKeyword())){
            lambdaQueryWrapper.and(c -> c.like(
                            AdminSpecial::getTitle,adminSpecialSearchPO.getKeyword())
                    .or().like(AdminSpecial::getNickname, adminSpecialSearchPO.getKeyword())
                    .or().like(AdminSpecial::getUuid, adminSpecialSearchPO.getKeyword())
                    .or().like(AdminSpecial::getTags, adminSpecialSearchPO.getKeyword())
                    .or().like(AdminSpecial::getCategoryPname, adminSpecialSearchPO.getKeyword())
                    .or().like(AdminSpecial::getCategoryName, adminSpecialSearchPO.getKeyword())

            );
        }

        lambdaQueryWrapper.orderByDesc(AdminSpecial::getCreateTime);
        Page<AdminSpecial> adminSpecialPage = this.page(page, lambdaQueryWrapper);
        Page<AdminSpecialVo> adminSpecialVoPage = tranferPageBo(adminSpecialPage, AdminSpecialVo.class);
        return adminSpecialVoPage;
    }

    /**
     * 回收站
     * @param adminSpecialSearchPO
     * @return
     */
    public Page<AdminSpecialVo> findAdminSpecialsRecyle(AdminSpecialSearchPO adminSpecialSearchPO){
        Page<AdminSpecial> page = new Page<>(adminSpecialSearchPO.getPageNo(),adminSpecialSearchPO.getPageSize());
        LambdaQueryWrapper<AdminSpecial> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(AdminSpecial.class,c -> !c.getColumn().equalsIgnoreCase("content"));
        lambdaQueryWrapper.eq(AdminSpecial::getIsDeleted, DeletedEnums.DELETED.getCode());
        lambdaQueryWrapper.eq(AdminSpecial::getSystemId,SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialSearchPO.getStatus()), AdminSpecial::getStatus,adminSpecialSearchPO.getStatus());
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialSearchPO.getSpecialtype()), AdminSpecial::getSpecialtype,adminSpecialSearchPO.getSpecialtype());
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialSearchPO.getNewFlag()), AdminSpecial::getNewFlag,adminSpecialSearchPO.getNewFlag());
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialSearchPO.getHotFlag()), AdminSpecial::getHotFlag,adminSpecialSearchPO.getHotFlag());
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialSearchPO.getPushFlag()), AdminSpecial::getPushFlag,adminSpecialSearchPO.getPushFlag());
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialSearchPO.getCategoryPid()), AdminSpecial::getCategoryPid,adminSpecialSearchPO.getCategoryPid());
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialSearchPO.getCategoryId()), AdminSpecial::getCategoryId,adminSpecialSearchPO.getCategoryId());
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialSearchPO.getCommentFlag()), AdminSpecial::getCommentFlag,adminSpecialSearchPO.getCommentFlag());
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialSearchPO.getMode()), AdminSpecial::getMode,adminSpecialSearchPO.getMode());
        lambdaQueryWrapper.eq(Objects.nonNull(adminSpecialSearchPO.getVipFlag()), AdminSpecial::getVipFlag,adminSpecialSearchPO.getVipFlag());
        // 模糊匹配
        if(StringUtils.isNotEmpty(adminSpecialSearchPO.getKeyword())){
            lambdaQueryWrapper.and(c -> c.like(
                            AdminSpecial::getTitle,adminSpecialSearchPO.getKeyword())
                    .or().like(AdminSpecial::getNickname, adminSpecialSearchPO.getKeyword())
                    .or().like(AdminSpecial::getUuid, adminSpecialSearchPO.getKeyword())
                    .or().like(AdminSpecial::getTags, adminSpecialSearchPO.getKeyword())
                    .or().like(AdminSpecial::getCategoryPname, adminSpecialSearchPO.getKeyword())
                    .or().like(AdminSpecial::getCategoryName, adminSpecialSearchPO.getKeyword())

            );
        }

        lambdaQueryWrapper.orderByDesc(AdminSpecial::getCreateTime);
        Page<AdminSpecial> adminSpecialPage = this.page(page, lambdaQueryWrapper);
        Page<AdminSpecialVo> adminSpecialVoPage = tranferPageBo(adminSpecialPage, AdminSpecialVo.class);
        return adminSpecialVoPage;
    }

    /**
     * 保存和更新
     * @param adminSpecialSavePO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public AdminSpecialVo saveUpdateAdminSpecial(AdminSpecialSavePO adminSpecialSavePO){
        AdminSpecial adminSpecial = tranferBo(adminSpecialSavePO, AdminSpecial.class);
        adminSpecial.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminSpecial.setSystemName(SystemIdThrealLocal.get().getSystemName());
        boolean b = this.saveOrUpdate(adminSpecial);
        AdminSpecialVo adminSpecialVo = tranferBo(adminSpecial, AdminSpecialVo.class);
        return adminSpecialVo;
    }

    /**
     * 根据ID获取明细
     * @param id
     * @return
     */
    public AdminSpecialVo getAdminSpecialById(Long id){
        AdminSpecial adminSpecial = this.getById(id);
        AdminSpecialVo adminSpecialVo = tranferBo(adminSpecial, AdminSpecialVo.class);
        return adminSpecialVo;
    }

    /**
     * 单个逻辑删除
     * @param id
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean deleteAdminSpecialById(Long id){
        boolean b =  this.updateStatus(id, AdminSpecial.Columns.IS_DELETED,1);
        return b;
    }

    /**
     * 单个恢复
     * @param id
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean recoverByIdAdminSpecial(Long id){
        boolean b =  this.updateStatus(id, AdminSpecial.Columns.IS_DELETED,0);
        return b;
    }

    /**
     * 单个物理删除
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean removeAdminSpecialById(Long id){
        boolean b = this.removeById(id);
        return b;
    }

    /**
     * 批量逻辑删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean bacthDeletesAdminSpecial(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminSpecial> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("is_deleted", DeletedEnums.DELETED.getCode());
        updateWrapper.in("id", ids);
        boolean b = this.update(updateWrapper);
        return true;
    }

    /**
     * 批量恢复
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRecoversAdminSpecial(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminSpecial> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("is_deleted", DeletedEnums.UN_DELETED.getCode());
        updateWrapper.in("id", ids);
        boolean b = this.update(updateWrapper);
        return b;
    }

    /**
     * 批量物理删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRemovesAdminSpecial(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        return b;
    }


    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean updateStatus(Long id,String column,Integer value){
        boolean b = super.updateStatus(id,column,value);
        return b;
    }

    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    public boolean updateStatusStr(Long id,String column,String value){
        boolean b = super.updateStatusStr(id,column,value);
        return b;
    }

    /**
     * 批量上架
     *
     * @param batchIdsPO
     * @return
     */
    @Override
    public Boolean batchDowns(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }

        // 把文章进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminSpecial> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", StatusEnums.UN_STATUS.getCode());
        updateWrapper.in("id", ids);
        return this.update(updateWrapper);
    }

    /**
     * 批量上架
     *
     * @param batchIdsPO
     * @return
     */
    @Override
    public Boolean batchUps(BatchIdsPO batchIdsPO) {
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }

        // 把文章进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        UpdateWrapper<AdminSpecial> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("status", StatusEnums.STATUS.getCode());
        updateWrapper.in("id", ids);
        return this.update(updateWrapper);
    }

    /**
     * 复制
     * @param opid
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean copyData(Long opid){
        AdminSpecial data = this.getById(opid);
        if(Objects.nonNull(data)){
            data.setId(null);
            data.setStatus(0);
            data.setPath(data.getPath()+new Date());
            data.setCreateTime(LocalDateTime.now());
            data.setUpdateTime(LocalDateTime.now());
            data.setTitle(data.getTitle()+"[副本]");
            this.save(data);
        }
        return true;
    }
}
